const db = require('../db/index');
const moment = require('moment');

// 获取产品类别和总价
exports.getCategoryAndTotalPrice = (req, res) => {
  // 获取产品类别的数组
  const getCategory = () => {
    return new Promise((resolve, reject) => {
      let sql = 'select * from setting where set_name="产品设置"';
      db.query(sql, (err, results) => {
        if (err) return reject(err);
        let set_value = results[0].set_value;
        // 将字符串转成数组对象
        let categoryArr = eval(`(${set_value})`);
        resolve(categoryArr);
      })
    })
  }

  // 获取产品类别对应的总价数组
  const getAllPrice = (product_category) => {
    return new Promise((resolve, reject) => {
      let sql = 'select product_all_price from product where product_category=?';
      db.query(sql, product_category, (err, results) => {
        if (err) return reject(err);
        let totalPrice = 0;
        for (let i = 0; i < results.length; i++) {
          totalPrice += results[i].product_all_price;
        }
        resolve(totalPrice);
      })
    })
  }

  // 获取产品类别和总价
  const getCategoryAndPrice = async () => {
    let categoryArr = await getCategory();
    let priceArr = [];
    for (let i = 0; i < categoryArr.length; i++) {
      const product_category = categoryArr[i];
      let totalPrice = await getAllPrice(product_category)
      priceArr.push(totalPrice);
    }
    res.send({
      status: 0,
      categoryArr,
      priceArr
    })
  }

  // 调用该接口
  getCategoryAndPrice();
}


// 获取管理员和数量
exports.getAdminAndCount = (req, res) => {
  // 获取管理员的数量
  const getCount = (identity) => {
    return new Promise((resolve, reject) => {
      let sql = 'select * from users where identity=?';
      db.query(sql, identity, (err, results) => {
        if (err) return reject(err);
        resolve(results.length);
      })
    })
  }

  // 获取用户数组
  const getAdminList = async () => {
    let userList = [{
        value: 0,
        name: '产品管理员'
      },
      {
        value: 0,
        name: '消息管理员'
      },
      {
        value: 0,
        name: '用户管理员'
      },
      {
        value: 0,
        name: '超级管理员'
      },
      {
        value: 0,
        name: '用户'
      }
    ]
    for (let i = 0; i < userList.length; i++) {
      const identity = userList[i].name;
      userList[i].value = await getCount(identity)
    }
    res.send({
      status: 0,
      userList
    })
  }

  // 调用该接口
  getAdminList();
}

// 获取消息等级和对应数量
exports.getMessageAndCount = (req, res) => {
  // 获取消息等级的数量
  const getCount = (message_level) => {
    return new Promise((resolve, reject) => {
      let sql = 'select * from message where message_level=?';
      db.query(sql, message_level, (err, results) => {
        if (err) return reject(err);
        resolve(results.length);
      })
    })
  }

  // 获取用户数组
  const getMessageList = async () => {
    let messageList = [{
        value: 0,
        name: '一般'
      },
      {
        value: 0,
        name: '必要'
      },
      {
        value: 0,
        name: '重要'
      }
    ]
    for (let i = 0; i < messageList.length; i++) {
      const message_level = messageList[i].name;
      messageList[i].value = await getCount(message_level)
    }
    res.send({
      status: 0,
      messageList
    })
  }

  // 调用该接口
  getMessageList();
}

// 返回每天登录的人数
exports.getEveryDayLogin = (req, res) => {
  // 获取最近七天日期
  const getDay = () => {
    let day = new Date();
    let week = [];
    for (let i = 0; i < 7; i++) {
      day.setDate(day.getDate() - 1);
      week.push(moment(day.toLocaleDateString().replace(/\//g, '-'), 'YYYY-MM-DD').format('YYYY-MM-DD'));
    }
    return week;
  }

  // 获取每天的登录次数
  const getLoginCount = (login_time) => {
    return new Promise((resolve, reject) => {
      let sql = `select * from login_log where login_time like '%${login_time}%'`;
      db.query(sql, login_time, (err, results) => {
        if (err) return reject(err);
        resolve(results.length);
      })
    })
  }

  // 获取登录次数
  const getLoginList = async () => {
    let weekList = getDay();
    
    let loginList = [];
    for (let i = 0; i < weekList.length; i++) {
      let count = await getLoginCount(weekList[i]);
      loginList.push(count);
    }
    res.send({
      status: 0,
      loginList
    })
  }

  // 调用接口
  getLoginList();
}